var myiscroll2;
var report_date="";
var pageData={};
$(document).ready(function(){
	//解析url,获取汇报日期
	/*
    var url = location.href.split('?')[1];
	if(url!=null){
		var arr = url.split('&');
		for(var i = 0 ; i < arr.length;i++){
			var temp = arr[i];
			var tempArr = temp.split('=');
			var key = tempArr[0];
			if(key == "report_date"){
				report_date=tempArr[1];
			}
		}
	}
	*/
	var barHeight = $("#J-TopBar").height();
	var fixed = $("#J-FixedTitle").parents("section.fixed-title");
	fixed.get(0).style.top = barHeight+"px";
	
	report_date = getQueryParam("report_date");
    if(report_date==""){
		var date = new Date();
		var month = date.getMonth()+1;
		var day = date.getDate();
		var year=date.getFullYear();
		if(day<10){
			if(month==1){
				month=12;
				year = year-1;
			}else
				month=month-1;
		}
		if(month<10)
			month="0"+month;
		report_date = year+"/"+month;
		$("#mydate").attr("value",report_date);
    }
    var mm = report_date.substr(5,2);
    var te ='我的' + mm + '月汇报'; 
	$("#reportDate").text('');
	$("#reportDate").text(te);
    romanceTag(report_date);
	
	$("#errorMask").on("tap",function(){
		$("#errorMask").hide();
	});
	//日期控件
	$('#mydate').scroller('destroy').scroller({
		preset: 'date',
		dateFormat: 'yy/mm',
		dateOrder: 'yymm',
		minDate: new Date(2002, 3, 10, 9, 22),
		maxDate: new Date(2030, 7, 30, 15, 44),
		theme: 'ios',
		mode: 'scroller',
		lang: 'zh',
		display: 'bottom',
		animate: 'slideup'
	});
	
	//汇报状态提示
	$("#report_status").on("tap",function(){
    	var status = pageData.report.pAIC_MON_STATUS;
    	var manager1_name = pageData.report.manager1_name;
    	var month="";
    	month = report_date.substr(5,2);
    	var message="";
    	if(status=="USUB")
    		message="您的"+month+"月汇报还未提交，提交后将由"+manager1_name+"审批。";
    	else if(status=="SUBM")
    		message="您的"+month+"月汇报已提交，将由"+manager1_name+"确认。";
    	else if(status=="APP")
    		message="您的"+month+"月汇报已由"+manager1_name+"确认。";
    	else if(status=="BAC")
    		message="您的"+month+"月汇报已被"+manager1_name+"驳回，重新提交后将由"+manager1_name+"审批";
    	else if(status=="NON")
    		message="您的"+month+"月汇报还未制定，制定后将由"+manager1_name+"审批。";
    	errorMask(message);
    });
    //跳转到查看评分页
	$("#J-BtnSave").on("tap", function(){
		//location.href="/mobile/myreport/wdydbg2-chakan.html?report_date="+ report_date;
		var param = "report_date="+ report_date;
		gotoLocation("/mobile/myreport/wdydbg2-chakan.html",param);
	});
	
	$("#toHome").on("tap",function(){
		gotoLocation("/mobile/index.html","");
	});
    
}); 

function romanceTag(report_date){//渲染页面标签
	$("#content").hide();
	$("#loading").show();
	hideBtn();
	$.ajax({
		type: "post",
		url: mpsurl+"/mobile/report/queryMyReport.do",
		dataType: "json",
		data:{
			report_date:report_date
		},
		success: function (data) { 
			$("#loading").hide();
			showBtn();
			$("#content").show();
			var obj = eval(data);
			var resultCode = obj.resultCode;
			var resultMessage = obj.resultMessage;
			
			if(resultCode=="500600"){
				$("#value").val(report_date);//传给子页面的数据
				
				pageData = obj.value;
				var kpi = obj.value.kpi; 
				var ew = obj.value.ew;    //非评分想数据集合
				var ewg = obj.value.ewg;  //评分想数据集合
				var ewn = obj.value.ewn;  //评分想数据集合
			 	var report = obj.value.report; 
			 	var reportGrade = obj.value.reportGrade; 
			 	
			 	//检测月度汇报  状态
				checkStatus(report.pAIC_MON_STATUS,report_date);
			  
				var i = $(".h1-icon.data-icon");
				var date = i.parent();
		 
				var pAIC_MON_STATUS =  report.pAIC_MON_STATUS; //月度汇报状态
				if(pAIC_MON_STATUS == "USUB"){
					$("#report_status").text("未提交");  
				}else if(pAIC_MON_STATUS == "SUBM"){
					$("#report_status").text("待确认");  
				}else if(pAIC_MON_STATUS == "APP"){
					$("#report_status").text("已确认");  
				}else if(pAIC_MON_STATUS == "BAC"){
					$("#report_status").text("已驳回");  
				}else if(pAIC_MON_STATUS == "NON"){
					$("#report_status").text("未制定");  
				}else{
					$("#report_status").text("未制定");
				}
				
				$("#reportDate").val(report_date);//查看评分传入值
				
				$("#klist").empty();//用户调整日期后删除上一个日期的数据
				$("#plist").empty();
				//KPI
				if(kpi.length>0){
					for(var i=0; i<kpi.length; i++){
						var content_descr = kpi[i].content_descr;  //kpi标题标题
						var paic_accmp_sta = kpi[i].paic_accmp_sta.replace(/\n/g,"<br/>");//完成情况
						var paic_cur_trgt = kpi[i].paic_cur_trgt;  //预期目标
						var section_id = kpi[i].section_id;  
	
						//创建出一条KPI记录
						div = $("<div class=\"kpi-main m-t18\">"+
						                "<h3 class=\"kpi-title icon-click\">"+
					                		"<div class=\"row-left-icon\"><i>"+(i+1)+"</i></div>"+
							                "<div class=\"info-list-cont\">"+content_descr+"</div>"+
							                "<div class=\"info-list-icon\"></div>"+ 
							            "</h3>"+
							            "<div class=\"kpi-content row-info edit-row row-info-readonly\">"+
							                "<ul class=\"row-info-list\">"+
							                    "<li>"+
							                        "<div class=\"info-list-name\">预期目标：</div>"+
							                        "<div class=\"info-list-cont\">"+paic_cur_trgt+"</div>"+
							                    "</li>"+
							                    "<li>"+
							                        "<div class=\"info-list-name\">本月完成：</div>"+
							                        "<div class=\"info-list-cont\">"+paic_accmp_sta+"</div>"+
							                    "</li>"+                        
							                "</ul>"+
							            "</div>"+
							         "</div>");
				        
						$("#klist").append(div);
						iconTitleEvent(div);
					}
				}
				//ew重要工作计划  非评分项
				if(ew.length>0){
					for(var i=0; i<ew.length; i++){
						var ew_descr = ew[i].ew_descr;//标题
						
						var ew_nco = ew[i].ew_nco.replace(/\n/g,"<br/>"); //下月行动
						var ew_rea = ew[i].ew_rea.replace(/\n/g,"<br/>"); //实际完成工作
						var ew_section_id = ew[i].ew_section_id;//重要工作计划的ID
						var ew_status = ew[i].ew_status; //正在进行IP，已完成CO
						var ew_statusVo = ew_status; //正在进行IP，已完成CO
						 
						
						var ew_sum = ew[i].ew_sum.replace(/\n/g,"<br/>"); //阶段汇总
						var ew_up_nco = ew[i].ew_up_nco; //上月安排
						var trk_status = ew[i].trk_status; //状态中文
						var paic_expectation = ew[i].paic_expectation;  //预期目标，不用显示保存时要提交
						
						var li = $("<li>"+
				                        "<div class=\"info-list-name\"></div>"+
				                        "<div class=\"info-list-cont\"></div>"+
			                       "</li>");
						if(ew_status == "IP"){
							ew_statusVo = "正在进行"
							$(li).children("div.info-list-name").text("下月行动：");
							$(li).children("div.info-list-cont").html(ew_nco.replace(/\n/g,"<br>"));
						}else if(ew_status == "CO"){
							ew_statusVo = "已完成";
							$(li).children("div.info-list-name").text("阶段汇总：");
							$(li).children("div.info-list-cont").html(ew_sum.replace(/\n/g,"<br>"));
						}
						var ncostr = "<li>"+
					                        "<div class=\"info-list-name\">上月安排：</div>"+
					                        "<div class=\"info-list-cont\">"+ew_up_nco+"</div>"+
					                    "</li>";
						if(ew_up_nco == "" || ew_up_nco == null){
							ncostr = "";
						}
						div = $("<div class=\"kpi-main m-t18\">"+
								        "<h3 class=\"kpi-title icon-click\">"+
							                "<div class=\"row-left-icon\"><i>"+(i+1)+"</i></div>"+
							                "<div class=\"info-list-cont\">"+ew_descr+"</div>"+ 
							                "<div class=\"info-list-icon\"></div>"+ 
							            "</h3>"+
							            "<div class=\"kpi-content row-info edit-row row-info-readonly\">"+
							                "<ul class=\"row-info-list\">"+
							                    ncostr+
							                    "<li>"+
							                        "<div class=\"info-list-name\">实际完成：</div>"+
							                        "<div class=\"info-list-cont\">"+ew_rea+"</div>"+
							                    "</li>"+  
						                        "<li>"+
							                        "<div class=\"info-list-name\">完成情况：</div>"+
							                        "<div class=\"info-list-cont\">"+ew_statusVo+"</div>"+
						                        "</li>"+ 
							                "</ul>"+
							            "</div>"+
							        "</div>");
						 
						$(div).find("li").last().after(li);
						$(div).find("li").slice(2).hide();
						$("#plist").append(div);
						iconTitleEvent(div);
					} 
				}
				//ewn  新增重要工作计划
				if(ewn.length>0){
					for(var i=0; i<ewn.length; i++){
						var ewn_section_id = ewn[i].ewn_section_id;//重要工作计划的ID
						var ewn_descr = ewn[i].ewn_descr.replace(/\n/g,"<br/>");//标题
						var ewn_nco = ewn[i].ewn_nco.replace(/\n/g,"<br/>"); //下月行动
						var ewn_rea = ewn[i].ewn_rea.replace(/\n/g,"<br/>"); //实际完成工作
						var ewn_status = ewn[i].ewn_status; //正在进行IP，已完成CO
						var ewn_statusVo = ewn_status;  
						
						var ewn_sum = ewn[i].ewn_sum.replace(/\n/g,"<br/>"); //阶段汇总
						var ewn_up_nco = ewn[i].ewn_up_nco; //上月安排
						var paic_expectation = ewn[i].paic_expectation;  //预期目标，不用显示保存时要提交
						
						var li = $("<li>"+
				                        "<div class=\"info-list-name\"></div>"+
				                        "<div class=\"info-list-cont\"></div>"+
			                       "</li>");
						if(ewn_status == "IP"){
							ewn_statusVo = "正在进行"
							$(li).children("div.info-list-name").text("下月行动：");
							$(li).children("div.info-list-cont").html(ewn_nco.replace(/\n/g,"<br>"));
						}else if(ewn_status == "CO"){
							ewn_statusVo = "已完成";
							$(li).children("div.info-list-name").text("阶段汇总：");
							$(li).children("div.info-list-cont").html(ewn_sum.replace(/\n/g,"<br>"));
						}
						var ncostr = "<li>"+
					                        "<div class=\"info-list-name\">上月安排：</div>"+
					                        "<div class=\"info-list-cont\">"+ewn_up_nco+"</div>"+
					                    "</li>";
						if(ewn_up_nco == "" || ewn_up_nco == null){
							ncostr = "";
						}
						
						div = $("<div class=\"kpi-main m-t18\">"+
								        "<h3 class=\"kpi-title icon-click\">"+
							                "<div class=\"row-left-icon\"><i>"+(ew.length+1+i)+"</i></div>"+
							                "<div class=\"info-list-cont\">"+ewn_descr+"</div>"+ 
							                "<div class=\"info-list-icon\"></div>"+ 
							            "</h3>"+
							            "<div class=\"kpi-content row-info edit-row row-info-readonly\">"+
							                "<ul class=\"row-info-list\">"+
							                    ncostr+
							                    "<li>"+
							                        "<div class=\"info-list-name\">实际完成：</div>"+
							                        "<div class=\"info-list-cont\">"+ewn_rea+"</div>"+
							                    "</li>"+  
						                        "<li>"+
							                        "<div class=\"info-list-name\">完成情况：</div>"+
							                        "<div class=\"info-list-cont\">"+ewn_statusVo+"</div>"+
						                        "</li>"+ 
							                "</ul>"+
							            "</div>"+
							        "</div>");
						$(div).find("li").last().after(li);
						$(div).find("li").slice(2).hide();
						$("#plist").append(div);
						iconTitleEvent(div);
					}
				}
				//ewg 重要工作计划  评分
				if(ewg.length>0){
					for(var i=0; i<ewg.length; i++){
						var ewg_section_id = ewg[i].ewg_section_id;//id
						var ewg_descr = ewg[i].ewg_descr;//标题
						var ewg_up_nco = ewg[i].ewg_up_nco; //上月安排
						var ewg_nco = ewg[i].ewg_nco.replace(/\n/g,"<br/>"); //下月行动
						var ewg_rea = ewg[i].ewg_rea.replace(/\n/g,"<br/>"); //实际完成工作
						var ewg_sum = ewg[i].ewg_sum.replace(/\n/g,"<br/>"); //阶段汇总
						var ewg_status = ewg[i].ewg_status; //正在进行IP，已完成CO
						var ewg_statusVo = ewg_status //正在进行IP，已完成CO 
						var ewg_weight = ewg[i].ewg_weight; //权重
						var ewg_self_rating = ewg[i].ewg_self_rating; //自评分
						var ewg_oth_rating = ewg[i].ewg_oth_rating; //领导评分 
						var paic_expectation = ewg[i].paic_expectation;  //预期目标，不用显示保存时要提交
						
						var li = $("<li>"+
				                        "<div class=\"info-list-name\"></div>"+
				                        "<div class=\"info-list-cont\"></div>"+
			                       "</li>");
						if(ewg_status == "IP"){
							ewg_statusVo = "正在进行"
								$(li).children("div.info-list-name").text("下月行动：");
							$(li).children("div.info-list-cont").html(ewg_nco.replace(/\n/g,"<br>"));
						}else if(ewg_status == "CO"){
							ewg_statusVo = "已完成";
							$(li).children("div.info-list-name").text("阶段汇总：");
							$(li).children("div.info-list-cont").html(ewg_sum.replace(/\n/g,"<br>"));
						}
						var strewg="";
						if(ewg[i].iscyc=="1"){
							if(ewg_status == "CO"&&ewg[i].ewg_status_app=="0"&&ewg[i].ewg_status_app!=2){
								strewg ="<li class=\"col-multi\">"+
							       "<div class=\"row-webkit-box col-multi-1\">"+
							       "<div class=\"info-list-name\">自评分：</div>"+
							       "<div class=\"info-list-cont\">"+ewg_self_rating+" <span class=\"fn-c-ccc\">分</span></div>"+
							       "</div>"+
							       "<div class=\"row-webkit-box col-multi-2\">"+
							       "<div class=\"info-list-name\">评分：</div>"+
							       "<div class=\"info-list-cont\">"+ewg_oth_rating+"<span class=\"fn-c-ccc\">分</span></div>"+
							       "</div>"+
							       "</li>";
							}else{
								if(pAIC_MON_STATUS=="APP")
									strewg ="<li class=\"col-multi\">"+
								       "<div class=\"row-webkit-box col-multi-1\">"+
								       "<div class=\"info-list-name\">自评分：</div>"+
								       "<div class=\"info-list-cont\">"+ewg_self_rating+" <span class=\"fn-c-ccc\">分</span></div>"+
								       "</div>"+
								       "<div class=\"row-webkit-box col-multi-2\">"+
								       "<div class=\"info-list-name\">评分：</div>"+
								       "<div class=\"info-list-cont\">"+ewg_oth_rating+"<span class=\"fn-c-ccc\">分</span></div>"+
								       "</div>"+
								       "</li>";
								else{
									strewg ="<li class=\"col-multi\">"+
								       "<div class=\"row-webkit-box col-multi-1\">"+
								       "<div class=\"info-list-name\">自评分：</div>"+
								       "<div class=\"info-list-cont\">"+ewg_self_rating+" <span class=\"fn-c-ccc\">分</span></div>"+
								       "</div>"+
								       "</li>";
								}
							}
						}
						var ncostr = "<li>"+
					                        "<div class=\"info-list-name\">上月安排：</div>"+
					                        "<div class=\"info-list-cont\">"+ewg_up_nco+"</div>"+
					                    "</li>";
						if(ewg_up_nco == "" || ewg_up_nco == null){
							ncostr = "";
						}
						div = $("<div class=\"kpi-main m-t18\">"+
								        "<h3 class=\"kpi-title icon-click\">"+
							                "<div class=\"row-left-icon\"><i>"+(i+ew.length+ewn.length+1)+"</i></div>"+
							                "<div class=\"info-list-cont\">"+ewg_descr+"</div>"+ 
							                "<div class=\"info-list-icon\"></div>"+ 
							            "</h3>"+
							            "<div class=\"kpi-content row-info edit-row row-info-readonly\">"+
							                "<ul class=\"row-info-list\">"+
							                    ncostr+
							                    "<li>"+
							                        "<div class=\"info-list-name\">实际完成：</div>"+
							                        "<div class=\"info-list-cont\">"+ewg_rea+"</div>"+
							                    "</li>"+  
						                        "<li>"+
							                        "<div class=\"info-list-name\">完成情况：</div>"+
							                        "<div class=\"info-list-cont\">"+ewg_statusVo+"</div>"+
						                        "</li>"+ 
//						                        "<li class=\"col-multi\" id=\"ewg_self_rating\">"+
//									                        "<div class=\"row-webkit-box col-multi-1\">"+
//										                        "<div class=\"info-list-name\">自评分：</div>"+
//										                        "<div class=\"info-list-cont\">"+ewg_self_rating+" <span class=\"fn-c-ccc\">分</span></div>"+
//									                        "</div>"+
	//						                        "<div class=\"row-webkit-box col-multi-2\">"+
	//						                            "<div class=\"info-list-name\">评分：</div>"+
	//						                            "<div class=\"info-list-cont\">"+ewg_oth_rating+"<span class=\"fn-c-ccc\">分</span></div>"+
	//						                        "</div>"+
													strewg +
//							                    "</li>"+
							                "</ul>"+
							            "</div>"+
							        "</div>"); 
						$(div).find("li").eq(2).after(li);
						$(div).find("li").slice(2).hide();
						$("#plist").append(div);
						iconTitleEvent(div);
						if(ewg[i].iscyc=="0"){
			                $("#ewg_self_rating").hide();
			            }
					}
				}
				iscroll();
			}else{
				//alert(resultMessage);
				$("#loading").hide();
				$("#content").hide();
				hideBtn();
				var tourl = "/mobile/myreport/wdydbg-chakan.html?report_date="+report_date;
            	checkLogin(obj.resultCode,tourl,resultMessage);
				//errorMask(resultMessage||"网络连接异常");
			}
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			//alert(errorThrown);
			$("#loading").hide();
			$("#content").hide();
			hideBtn();
			//errorMask("网络连接异常");
			setPromptForInput("网络异常");
		}
	}); 
}
/**
 *  检测月度汇报状态   3-17
 */
function checkStatus (status,report_date) {
	//{"USUB":"未提交","SUBM":"待确认","APP":"已确认","BAC":"驳回","NON":"未制定"};
    if(status=="USUB" || status=="BAC" || status=="NON"){
    	//location.href="/mobile/kpi/kpi-todo.html?report_date="+report_date;
    	var param = "report_date="+ report_date;
		gotoLocation("/mobile/kpi/kpi-todo.html",param);
    	return ;
    }
}

/**
 * 小图标点击事件
 */
function iconTitleEvent(divs){
	icon = $(divs).find("div.info-list-icon");//截取上下小图标
   	icon.on("tap",function(){ //小图标点击事件
    	var divp = $(this).parent().parent(); 
        divp.toggleClass(function(){
        	var classSize = divp.attr("class").split(" ").length;
    		if(classSize==2){
    			$(divp).find("li").slice(2).show();
    			divp.removeClass("kpi-main m-t18");
    			return "kpi-main m-t18 open-kpi-cont";
    		}else{
    			$(divp).find("li").slice(2).hide();
    			divp.removeClass("kpi-main m-t18 open-kpi-cont");
    			return "kpi-main m-t18";
    		}
    	}); 
        iscroll();
        myiscroll2.scrollToElement(this,0);
    });

   	title = $(div).children("h3").children("div.info-list-cont"); //得到记录标题
   	title.on("tap", function(){//标题点击事件
   		var p = $("#GlobalMask").find("section.g-mask-cont").children("p");
       	p.text($(this).text());
      	$("#GlobalMask").show();
    });
}


// 更改时间，并查询
function change() {
	report_date = $("#mydate").val();
	//var yy = da.substring(0, 4);
	var mm = report_date.substr(5, 2);
	//report_date = yy + "/" + mm;
	var te ='我的' + mm + '月汇报'; 
	$("#reportDate").text('');
	$("#reportDate").text(te); 
	romanceTag(report_date);
}

//显示时间选择插件
function show() { 
	$("#mydate").scroller('show');
	$(".dwo").css("background-color","#000");
    $(".dwo").css("opacity",".5");
    $(".dwo").css("z-index","1000");
}
$(document).on("tap", function(){
    $(".dw dwbg").on("tap",function(){
    	return;
    })
    $(".dwo").on("tap",function(){
    	$("#promptBox").remove("slow");	
    })
    $(".dwbw dwb-c").on("tap",function(){
    	$("#promptBox").remove("slow");	
    })
    $(".dwbw dwb-s").on("tap",function(){
    	$("#promptBox").remove("slow");	
    })
});

function iscroll(){
	var wh = $(window).height()-90-112; 
    if(myiscroll2 instanceof iScroll){
    	myiscroll2.destroy();
    }
    $("#content").css("height",wh+"px");    
    myiscroll2 = new iScroll("content",{
        bounce: false,
        anchorOpen: true,
        anchorElem: $("h2"),
        onAnchor: function(n){
            if(n === -1){
                $("#J-FixedTitle").parent().hide();
            }else{
                $("#J-FixedTitle").parent().show();
                $("#J-FixedTitle").html(this.options.anchorElem.eq(n).children().html())
            }
        }
    });
}
function showBtn(){
	$("#J-BtnSave").show();
	$("#NoJ-BtnSave").hide();
}
function hideBtn(){
	$("#J-BtnSave").hide();
	$("#NoJ-BtnSave").show();
}
